import 'package:whale/helper/component/download/state.dart';

import '../../../helper/component/download/controller.dart';
import '../../../helper/utils/util.dart';
import 'index.dart';

class MutiDownload_2Controller extends GetxController {
  MutiDownload_2Controller();

  final state = MutiDownload_2State();

  void onCellChoose(DownloadModel model) {
    if (state.editList.contains(model)) {
      state.editList.remove(model);
    } else {
      state.editList.add(model);
    }

    update(['${model.task.taskId}-choose', 'all-choose']);
  }

  void onAllChoose() {
    if (state.model.data?.isNotEmpty != true) {
      return;
    }
    if (state.editList.length == state.model.data?.length) {
      state.editList = [];
    } else {
      state.editList.addAll(state.model.data ?? []);
    }
    update();
  }

  void onAllPause() async {
    await DownloadController.instance.pauseAll();
    update();
  }

  void onAllRemove() async {
    await DownloadController.instance.removeFile(state.model);
    Get.back(result: 'update');
  }

  void onRemove() async {
    if (state.editList.length == state.model.data?.length) {
      onAllRemove();
    } else {
      await DownloadController.instance.removeArr(state.model, state.editList);
      Get.back(result: 'update');
    }
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.model = Get.arguments;
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    // DownloadController.instance.progressCallback = (model) {
    //   if (state.model.name == model.fileName) {
    //     update();
    //   }
    // };
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
